import ujson
import datetime
import time

from 获取监测点信息 import *
from 操作消息队列 import *

def msg(**kwargs):
    # 获取当前时间
    now = datetime.datetime.now()
    nodes = select_scadaequipnodeinfo(kwargs['equipmentNumber'])
    acode = kwargs['acode']
    values = kwargs['values'].split(',')

    json = {}
    # 根据nodes的元素个数进行循环
    for i in range(len(nodes)):
        # 获取第i个元素
        node = nodes[i]
        nodeid = node[1]
        if nodeid == '00000000000000000001':
            json[nodeid] = '1'
        else:
            json[nodeid] = values[i]

    json['DATETIME_ACQUISITION'] = str(now)
    json['DEVICE_CODE'] = acode

    if 'workLocation' in kwargs and kwargs['workLocation'] != '':
        json['WORK_LOCATION'] = kwargs['workLocation']

    if 'testFormNumber' in kwargs and kwargs['testFormNumber'] != '':
        json['TESTORMNUMBER'] = kwargs['testFormNumber']

    json_str = ujson.dumps(json)
    return json_str

def send_msg(**kwargs):
    json_str = msg(**kwargs)
    queueName = kwargs['queueName']
    # 上面的发送消息代码，循环调用100次
    connection,channel = conn_rabbitmq_guian()
    for i in range(int(kwargs['count'])):
        channel.basic_publish(exchange='', routing_key=queueName, body=json_str)
        # 输出显示：发送完成：json_str
        print(str(i)+"发送完成：" + json_str)
    # 关闭连接
    connection.close()


if __name__ == '__main__':

    # 无限循环
    while True:
        # 延时10秒
        time.sleep(10)
        params = {
            'equipmentNumber':'TA2211080005160022010106',
            'acode':'A2211080005160022',
            'queueName':'prod_jd_equip_warn_msg',
            # 'queueName':'test_queue_2',
            'workLocation':'',
            'testFormNumber':'',
            'count':'100',
            'values':'0.1,0.2,0.3,0.4,0.5,0.6,0.7,8,9,0,0.1,0.2,0,0,0,0,0,0,0,0,0,0,0,0,0,0'
        }
        send_msg(**params)

        # params = {
        #     'equipmentNumber': '虚拟设备2',
        #     'acode': '虚拟设备2',
        #     'queueName': 'test_queue',
        #     'workLocation': 'ZE-15-3',
        #     'testFormNumber': 'ST2024080023',
        #     'count': '100',
        #     'values': '0.1,0.2,0.3,0.4,0.5,0.6,0.7,8,9,0,0.1,0.2,0,0,0,0,0,0,0,0,0,0,0,0,0,0'
        # }
        # send_msg(**params)
